package com.yupi.springbootinit.service.impl.search;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.enums.SearchKeyWord;
import com.yupi.springbootinit.model.entity.Post;
import com.yupi.springbootinit.model.vo.PostVO;
import com.yupi.springbootinit.service.PostService;
import com.yupi.springbootinit.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PostSearchService implements SearchService<PostVO> {

    @Resource
    PostService postService;

    @Override
    public Page<PostVO> doSearch(String searchText, int pageSize, int pageNo) {
        if (pageSize <= 0) pageSize = 1;
        if (pageNo <= 0) pageNo = 10;
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(searchText), "title", searchText);
        Page<Post> page = postService.page(new Page<>(pageSize, pageNo), queryWrapper);
        return PageUtils.convert(page, post -> new PostVO(post.getId(), post.getTitle(), post.getContent()));
    }

    @Override
    public SearchKeyWord getSearchKeyWord() {
        return SearchKeyWord.POST;
    }
}
